package org.nearby.note;

import java.awt.Component;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import org.nearby.note.bean.fastrun;
import org.nearby.note.dao.fastrunDaoImpl;
import org.nearby.note.dialog.AddFastRunDlg;
import org.nearby.note.model.DiyTableModel;

public class Fastrun extends JFrame implements ActionListener {
    private JButton addBtn;
    private JButton startBtn;
    private JLabel last_run_tip;

    private JTable runList;
    private DefaultTableModel defaultModel;

    private fastrunDaoImpl fastImpl = new fastrunDaoImpl();

    private JScrollPane scrollPane;

    private int selectedRow;

    private Component _self;

    public Fastrun() {
        _self = this;
        this.setTitle("一键启动");
        this.setSize(500, 500);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        initWin();
        initFillData();
        initBind();
    }

    public void initWin() {
        JPanel mainPane = new JPanel();
        mainPane.setLayout(null);

        /*
         * mainPane.setBounds(100, 100, 300, 300); Border b =
         * BorderFactory.createLineBorder(Color.red); mainPane.setBorder(b);
         */
        last_run_tip = new JLabel("还没有执行过哦！");
        last_run_tip.setBounds(40, 20, 150, 30);

        startBtn = new JButton("开始执行");
        startBtn.setBounds(200, 20, 100, 30);
        startBtn.setFocusPainted(false);

        addBtn = new JButton("添加一键启动");
        addBtn.setBounds(350, 20, 130, 30);
        addBtn.setFocusPainted(false);

        defaultModel = new DiyTableModel(fastImpl.getAllFastRun(), new Vector<String>(fastImpl
                .getColNames()));

        runList = new JTable(defaultModel);
        scrollPane = new JScrollPane(runList);
        scrollPane.setBounds(50, 80, 400, 300);

        mainPane.add(addBtn);
        mainPane.add(startBtn);
        mainPane.add(last_run_tip);
        mainPane.add(scrollPane);

        this.add(mainPane);
        this.setVisible(true);
        this.setResizable(false);
    }

    public void initFillData() {

    }

    public void initBind() {
        addBtn.addActionListener(this);

        // 给表格添加右键菜单
        final JPopupMenu tabPopUp = new JPopupMenu();
        JMenuItem delRow = new JMenuItem("删除运行项");
        tabPopUp.add(delRow);

        runList.addMouseListener(new MouseAdapter() {
            public void mouseReleased(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    selectedRow = runList.rowAtPoint(new Point(e.getX(), e.getY()));
                    if (selectedRow != -1) {
                        tabPopUp.show(runList, e.getX(), e.getY());
                    }
                }
            }

            public void mousePressed(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    selectedRow = runList.rowAtPoint(new Point(e.getX(), e.getY()));
                    if (selectedRow != -1) {
                        tabPopUp.show(runList, e.getX(), e.getY());
                    }
                }
            }
        });
        delRow.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int run_id = Integer.parseInt(defaultModel.getValueAt(selectedRow, 0).toString());
                if (fastImpl.delFastRun(run_id)) {
                    defaultModel.removeRow(selectedRow);
                    JOptionPane.showMessageDialog(_self, "删除成功");
                } else {
                    JOptionPane.showMessageDialog(_self, "删除失败");
                }
            }
        });
        // 执行程序
        startBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Vector<Vector<Object>> data = defaultModel.getDataVector();
                String path;
                Runtime run = Runtime.getRuntime();

                for (int i = 0; i < data.size(); i++) {
                    path = data.get(i).get(2).toString();
                    if (path != null) {
                        try {
                            run.exec("cmd /c start " + path);
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }
                    }
                }
            }
        });
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("添加一键启动")) {
            JDialog addFastRun = new AddFastRunDlg(this, "新增一键启动", true);
            addFastRun.setVisible(true);
        }
    }

    // 更新表格
    public void updateTableUI(ArrayList<Integer> runIds) {
        // 如果有添加，添加到表格中
        if (runIds.size() != 0) {
            Iterator<Integer> runs = runIds.iterator();
            int run_id = 0;
            fastrun fr = new fastrun();
            while (runs.hasNext()) {
                run_id = runs.next();
                fr = fastImpl.getFastRunById(run_id);
                defaultModel.addRow(fr.convertVector());
            }
        }
    }

    public static void main(String[] args) {
        new Fastrun();
        /*
         * try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch
         * (Exception e) { e.printStackTrace(); }
         */
    }
}
